//      mime.c
//      
//      Copyright 2011 DJ Shaji <djshaji@gmail.com>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.


#include "mime.h"

gchar * mime_get_command ( gchar * filename )
{
	gchar * db = null ;
	if ( g_file_test ( SYSTEM_MIME_DB, G_FILE_TEST_EXISTS ))
		db = g_strdup ( SYSTEM_MIME_DB ) ;
	else
	{
		db = g_build_filename ( g_get_home_dir (), ".system", "mime.db", null ) ;
		if ( ! g_file_test ( db, G_FILE_TEST_EXISTS ))
		{
			g_free ( db ) ;
			
			g_critical ( "Cannot find mime database !" ) ;
			return null ;
		}
	}
	
	GKeyFile * file = g_key_file_new () ;
	GError * error = null ;
	
	g_key_file_load_from_file ( file, db, G_KEY_FILE_NONE, & error ) ;
	if ( error )
	{
		g_critical ( "Cannot load mime database: %s", error -> message ) ;
		g_free ( db ) ;
		
		g_key_file_free ( file ) ;
		return null ;
	}

	gsize size = 0, i = 0 ;
	gchar ** groups = g_key_file_get_groups ( file, & size ) ;
	
	gchar * command = null ;
	
	for ( i = 0 ; i < size ; i ++ )
	{
		command = g_key_file_get_string ( file, groups [i], "exec", null ) ;
		
		if ( command == null )
			continue ;
		
		if ( g_str_has_suffix ( filename, groups [i] ))
		{
			g_key_file_free ( file ) ;
			g_strfreev ( groups ) ;
			
			g_free ( db ) ;
			return command ;
		}
		
		g_free ( command ) ;
		command = null ;
	}
	
	g_key_file_free ( file ) ;
	g_free ( db ) ;
	
	g_strfreev ( groups ) ;
	return command ;
}

void mime_handler ( Explorer * e, gchar * filename )
{
	if ( g_file_test ( filename, G_FILE_TEST_IS_DIR ))
	{
		fm_open ( e, filename ) ;
		return ;
	}
	
	else if ( g_file_test ( filename, G_FILE_TEST_IS_EXECUTABLE ))
	{
		 Response r = prompt_question ( 			"This file seems to be a program. "
									"Do you want to run it instead of "
									"opening?\n\n"
									"Malicious programs can cause damage "
									"to the system and your data." ) ;
	
		switch ( r )
		{
			case YES:
				run_command ( filename ) ;
				return ;
			case CANCEL:
				return ;
			case NO:
			default:
				break ;
		}
	
	}

	explorer_set_status_message ( e, filename ) ;
	
	gchar * cmd = mime_get_command ( filename ) ;
	if ( cmd == null )
		g_warning ( "Unknown filetype !" ) ;
	else
	{
		GError * error = null ;
		gchar * command = g_strdup_printf ( "%s;%s", cmd, filename ) ;
		
		gchar ** vector = g_strsplit ( command, ";", -1 ) ;
		g_spawn_async ( null, vector, null, G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
						null, null, null, & error ) ;
		
		if ( error )
			g_critical ( "Cannot open %s! %s", filename, error -> message ) ;
		
		g_strfreev ( vector ) ;
		g_free ( command ) ;
	}
	
	g_free ( cmd ) ;
}
